package com.jnxx.task.WxUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class MessageUtils {
    @Autowired
    private WeChatUtils weChatUtils;
    @Autowired
    private TokenUtils tokenUtils;
    @Autowired
    private RedisTemplate<String ,Object> redisTemplate;
    public Map<Integer,String> getDepartmentIdToName(){
        List<Map<String,Object>> list=weChatUtils.getDepartmentListAndUserDetails(tokenUtils.getAccessToken());
        Map<Integer, String> departmentIdToName=new HashMap<>();
        for (Object department:list){
            Map<String ,Object> map= (Map<String, Object>) department;
            departmentIdToName.put(Integer.parseInt(map.get("id").toString()),map.get("name").toString());
        }
        return departmentIdToName;
    }

    public Map<String ,Object> getUserByUserId(String userId){
        Map<String ,Object> user=(Map<String ,Object>)redisTemplate.opsForValue().get("user:"+userId);
        if (user==null){
            weChatUtils.getDepartmentListAndUserDetails(tokenUtils.getAccessToken());
            user=(Map<String ,Object>)redisTemplate.opsForValue().get("user:"+userId);
            return user;
        }else {
            return user;
        }
    }
}
